<template>
  <div class="Realanme">
    <div class="bg">
      <van-image
        src="http://121.199.29.84:8888/group1/M00/00/56/rBD-SWDlWXmAbRnvAAIVL7QsewU787.jpg"
      />
      
    </div>
    <div class="form">
      <van-form :v-model="form">
        <van-field
          v-model="form.realname"
          name="真实姓名"
          label="真实姓名"
          placeholder="真实姓名"
          :rules="[{ required: true, message: '请填写真实姓名' }]"
        />
        <van-field
          v-model="form.idCard"
          name="身份证号"
          label="身份证号"
          placeholder="身份证号"
          :rules="[{ required: true, message: '请填写身份证号' }]"
        />
        <van-field
          v-model="form.bankCard"
          name="银行卡号"
          label="银行卡号"
          placeholder="银行卡号"
          :rules="[{ required: true, message: '请填写银行卡号' }]"
        />
        <van-field name="uploader" label="身份证正面照">
          <template #input>
            <van-uploader
              :after-read="afterRead1"
              v-model="fileList1"
              :max-count="1"
            />
          </template>
        </van-field>
        <van-field name="uploader" label="身份证反面照">
          <template #input>
            <van-uploader
              :after-read="afterRead2"
              v-model="fileList2"
              :max-count="1"
            />
          </template>
        </van-field>
        <van-field name="uploader" label="银行卡照片">
          <template #input>
            <van-uploader
              :after-read="afterRead3"
              v-model="fileList3"
              :max-count="1"
            />
          </template>
        </van-field>
        <div class="bt">
          
      <van-button
        round
        type="primary"
        size="large"
        color="linear-gradient(90deg,#7579ff,#bf73ff)"
        @click="submitHandler"
        >提交</van-button
      >
    </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { post_formData, post } from "../../http/axios";
import { Toast } from "vant";
export default {
    name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  data() {
    return {
      form: {},
      realname:"",
      idCard:"",
      bankCard:"",
      fileList1: [],
      fileList2: [],
      fileList3: [],
    };
  },
  methods: {
        afterRead1(file, detail) {
      console.log(this.dataURLtoFileFun(file.content));
      let formData = new FormData();
      formData.append('file',this.dataURLtoFileFun(file.content,'file.jpg'))
      post_formData('http://121.199.29.84:8001/file/upload',formData).then(res=>{
        // console.log(res.data);
        if(res.data.status === 200){
            this.form.idcardPhotoPositive ='http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id
            this.fileList1 = [{url:'http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id}]
        }
      })
    },
    afterRead2(file, detail) {
      console.log(this.dataURLtoFileFun(file.content));
      let formData = new FormData();
      formData.append('file',this.dataURLtoFileFun(file.content,'file.jpg'))
      post_formData('http://121.199.29.84:8001/file/upload',formData).then(res=>{
        console.log(res.data);
        if(res.data.status === 200){
            this.form.idcardPhotoNegative ='http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id
            this.fileList2 = [{url:'http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id}]
        }
      })
    },
    afterRead3(file, detail) {
      console.log(file);
      let formData = new FormData();
      formData.append('file',this.dataURLtoFileFun(file.content,'file.jpg'))
      post_formData('http://121.199.29.84:8001/file/upload',formData).then(res=>{
        console.log(res.data);
        if(res.data.status === 200){
            this.form.bankCardPhoto ='http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id
            this.fileList3 = [{url:'http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id}]
        }
      })
    },
    // bae64转文件对象
    dataURLtoFileFun (dataurl, fileName) {
      // 将base64转换为文件，dataurl为base64字符串，filename为文件名（必须带后缀名，如.jpg,.png）
      let arr = dataurl.split(",");
      let mime = arr[0].match(/:(.*?);/)[1];
      let bstr = atob(arr[1]);
      let n = bstr.length;
      let u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], fileName, { type: mime });
    },

    //提交审核
    submitHandler(){
      this.form.userId = this.$route.query.id
      console.log(this.form);
      post('/certification/submitCertificationApply',this.form).then(res=>{
        console.log(res.data.status);
        if(res.data.status === 200){
          Toast(res.data.message)
          this.$router.push('/manager/mine')
        }else {
          Toast(res.data.message)
        }
      })
    }
  },

  created () {},
  mounted () {},
  beforeDestroy () {},
  destroyed () {}
  };
</script>

<style scoped>
.bg {
  height: 5%;
  text-align: center;
  /* background-image: linear-gradient(#4568dc, #b06ab3); */
}
.bt {
  margin-top: 2%;
  text-align: center;
  width: 60%;
  margin-left: 20%;
  align-content: flex-end;
}
</style>